/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Note.java
 *
 * Created on May 16, 2011, 12:13:19 PM
 */
package views;

import controllers.NoteController;
import models.Note;

/**
 *
 * @author Steffen
 */
public class EditNoteView extends AbstractView {

    private NoteController noteCtr = null;
    
    /** Creates new form Note */
    public EditNoteView() {
        initComponents();
        noteCtr = new NoteController();
        
        hideChangesLabels();
    }
    
    public EditNoteView(int noteId) {
        initComponents();
        noteCtr = new NoteController(noteId);
        this.setValues();
    }
    
    public EditNoteView(Note note) {
        initComponents();
        noteCtr = new NoteController(note);
        this.setValues();
    }
    
    public void hideChangesLabels() {
        this.changedLabelsPanel.setVisible(false);
    }
    
    // Sætte værdierne i brugerfladen
    private void setValues() {
        if(noteCtr != null) {
            
            // Henter noten ud fra controlleren
            Note note = noteCtr.getNote();
            if(note != null) {
                
                // Sætter værdierne i felterne i brugerfladen
                this.headline.setText(note.getTitle());
                this.text.setText(note.getText());
                
                this.createdDate.setText(note.getCreated());
                this.createdBy.setText(note.getCreatedPerson().getName());
                this.updatedDate.setText(note.getUpdated());
                this.updatedBy.setText(note.getUpdatedPerson().getName());
                
                // Hakker feltet af om den er offentlig, hvis den er offentlig
                if(note.isPublic()) {
                    this.publicNote.setSelected(true);
                }
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headline = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        text = new javax.swing.JTextArea();
        publicNote = new javax.swing.JCheckBox();
        saveBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        changedLabelsPanel = new javax.swing.JPanel();
        updatedDate = new javax.swing.JLabel();
        updatedBy = new javax.swing.JLabel();
        createdDate = new javax.swing.JLabel();
        createdBy = new javax.swing.JLabel();
        textEdited = new javax.swing.JLabel();
        textCreated = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        headline.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        headline.setText("Overskrift");

        text.setColumns(20);
        text.setLineWrap(true);
        text.setRows(5);
        jScrollPane1.setViewportView(text);

        publicNote.setText("Offentlig");

        saveBtn.setText("Gem");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        jLabel3.setText("Notat");

        jButton1.setText("Annuller");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        updatedDate.setText("00-00-0000");

        updatedBy.setText("Brian Mikkelsen");

        createdDate.setText("00-00-0000");

        createdBy.setText("Henning Stærk");

        textEdited.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        textEdited.setText("Redigeret:");

        textCreated.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        textCreated.setText("Oprettet:");

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 14));
        jLabel1.setText("|");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 14));
        jLabel2.setText("|");

        org.jdesktop.layout.GroupLayout changedLabelsPanelLayout = new org.jdesktop.layout.GroupLayout(changedLabelsPanel);
        changedLabelsPanel.setLayout(changedLabelsPanelLayout);
        changedLabelsPanelLayout.setHorizontalGroup(
            changedLabelsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(changedLabelsPanelLayout.createSequentialGroup()
                .add(changedLabelsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(changedLabelsPanelLayout.createSequentialGroup()
                        .add(textEdited)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(updatedDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(updatedBy))
                    .add(changedLabelsPanelLayout.createSequentialGroup()
                        .add(textCreated)
                        .add(18, 18, 18)
                        .add(createdDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(createdBy)))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        changedLabelsPanelLayout.setVerticalGroup(
            changedLabelsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(changedLabelsPanelLayout.createSequentialGroup()
                .add(changedLabelsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(textCreated)
                    .add(createdDate)
                    .add(jLabel1)
                    .add(createdBy))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(changedLabelsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(textEdited)
                    .add(updatedDate)
                    .add(jLabel2)
                    .add(updatedBy)))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
                    .add(jLabel3)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(headline, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(publicNote))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(changedLabelsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(saveBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(headline, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(publicNote))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton1)
                        .add(saveBtn))
                    .add(changedLabelsPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Gemme den nye note
    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        
        // Er den offentlig?
        boolean isPublic = publicNote.isSelected() ? true : false;
        
        if(this.showConfirm("Er du sikker på at du vil gemme dette notat?")) {
            if(this.noteCtr.saveNote(this.headline.getText(), this.text.getText(), isPublic) > 0) {
                this.showMessage("Notatet er blevet gemt!");
                dispose();
            } else {
                this.showError("Der opstod en fejl. Notatet blev ikke gemt!");
            }
        }
    }//GEN-LAST:event_saveBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EditNoteView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel changedLabelsPanel;
    private javax.swing.JLabel createdBy;
    private javax.swing.JLabel createdDate;
    private javax.swing.JTextField headline;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox publicNote;
    private javax.swing.JButton saveBtn;
    private javax.swing.JTextArea text;
    private javax.swing.JLabel textCreated;
    private javax.swing.JLabel textEdited;
    private javax.swing.JLabel updatedBy;
    private javax.swing.JLabel updatedDate;
    // End of variables declaration//GEN-END:variables
}
